package com.hushui.mall.commodity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hushui.mall.commodity.entity.AttrAttrgroupRelationEntity;
import com.hushui.mall.commodity.service.AttrAttrgroupRelationService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * 属性&属性分组关联
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:46
 */
@RestController
public class AttrAttrgroupRelationController {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("commodity/attrattrgrouprelation/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrAttrgroupRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("commodity/attrattrgrouprelation/info/{id}")
    public R info(@PathVariable("id") Long id){
		AttrAttrgroupRelationEntity attrAttrgroupRelation = attrAttrgroupRelationService.getById(id);

        return R.ok().put("attrAttrgroupRelation", attrAttrgroupRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/attrattrgrouprelation/save")
    public R save(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation){
		attrAttrgroupRelationService.save(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/attrattrgrouprelation/update")
    public R update(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation){
		attrAttrgroupRelationService.updateById(attrAttrgroupRelation);

        return R.ok();
    }
    @RequestMapping("/product/attrgroup/attr/relation")
    public R addAttrRelation(@RequestBody ArrayList<AttrAttrgroupRelationEntity> relation){
        attrAttrgroupRelationService.saveBatch(relation);
        return R.ok();
    }
    @RequestMapping("/product/attrgroup/attr/relation/delete")
    public R delAttrRelation(@RequestBody ArrayList<AttrAttrgroupRelationEntity> relation){
        ArrayList<AttrAttrgroupRelationEntity> entities = new ArrayList<>();
        for (AttrAttrgroupRelationEntity a:relation
             ) {
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id",a.getAttrId()).eq("attr_group_id",a.getAttrGroupId());
            entities.add(attrAttrgroupRelationService.getOne(wrapper));
        }
        attrAttrgroupRelationService.removeByIds(entities);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("commodity/attrattrgrouprelation/delete")
    public R delete(@RequestBody Long[] ids){
		attrAttrgroupRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
